// pages/noauth/noauth.js
var app = getApp();
Page({
  data : {
    isPhone:false,
    phoneNumber:''
  },
  getAuth: function(res) {
    var that = this;
    wx.login({
      fail: function(res) {
        that.redirectIfNotPermit();
      },
      success: function(res) {
        that.saveUserInfo(res.code);
      }
    });

  },
  saveUserInfo: function(userCode) {
    var that = this;
    var loginActId = wx.getStorageSync("loginActId")
    console.log("loginActId---", loginActId)
    wx.getUserInfo({
      lang: 'zh_CN',
      fail: function(res) {
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 2000
        })
      },
      success: function(res) {
        console.log('获取信息',res)
        app.req({
          url: 'wechat/user/login',
          data: {
            encryptedData: res.encryptedData,
            iv: res.iv,
            code: userCode,
            loginActId: loginActId
          },
          method: 'POST',
          success: function(res) {
            if (res.code === 200) {
              wx.setStorageSync('uid', res.data.uid);
              that.setData({
                isPhone: true
              })
              // wx.navigateBack({
              //   delta: 1
              // })
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        });

      }
    });
  },
  getPhoneNumber: function (e) {//点击获取手机号码按钮
    var that = this;
    wx.login({
      success: (res) => {
        var ency = e.detail.encryptedData;
        var iv = e.detail.iv;
        var sessionk = that.data.sessionKey;
        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
          that.setData({
            modalstatus: true
          });
        } else {//同意授权
          console.log('获取手机号', res)
          app.req({
            method: "post",
            url: 'wechat/sysMessage/deciphering',
            data: {
              encrypdata: ency,
              ivdata: iv,
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: (res) => {
              if(res.code==200){
                var phone = res.data.telPhone;
                console.log('手机号', res)
                that.setData({ phoneNumber: phone });
                if (res.data.uid) {
                  wx.setStorageSync('uid', res.data.uid);
                }
                console.log('手机号获取', that.data.phoneNumber)
                if (that.data.phoneNumber) {
                  // that.preservePhone()
                  wx.removeStorageSync("loginActId")
                  console.log('手机号保存', that.data.phoneNumber)
                  that.setData({
                    isPhone: false
                  })
                  wx.setStorageSync('phone', that.data.phoneNumber);
                  wx.navigateBack({
                    delta: 1
                  })
              }
              } else {
                console.log('500', res)
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }, fail: function (res) {
              console.log(res);
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          });
        }
      }
    })
  },
  onLoad: function (options) {
    var that = this
    if (options.phone==0){
      that.setData({
        isPhone:true
      })
    }
  },
  onShow:function(){
  }
})